package com.asset.dispoal.assetInstitutionBatchParameter.service.commom;

import com.asset.dispoal.assetInstitutionBatchParameter.domain.db.entity.AssetInstitutionBatchParameter;
import com.asset.dispoal.assetInstitutionBatchParameter.domain.db.mapper.AssetInstitutionBatchParameterMapper;
import com.asset.dispoal.assetInstitutionBatchParameter.dto.AssetInstitutionBatchParameterRequest;
import com.asset.dispoal.assetInstitutionBatchParameter.dto.AssetInstitutionBatchParameterResponse;
import com.asset.dispoal.assetInstitutionBatchParameter.service.converter.AssetInstitutionBatchParameterConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/29$
 */
@Component
public class ParameterQueryPageCommand implements BaseCommand<AssetInstitutionBatchParameterRequest, Result<PageVO<AssetInstitutionBatchParameterResponse>>> {

    @Autowired
    AssetInstitutionBatchParameterMapper assetInstitutionBatchParameterMapper;

    @Override
    public Result<PageVO<AssetInstitutionBatchParameterResponse>> execute(AssetInstitutionBatchParameterRequest request) {
        AssetInstitutionBatchParameter assetInstitutionBatchParameter = AssetInstitutionBatchParameterConverter.INSTANCE.reqToDo(request);
        int count = assetInstitutionBatchParameterMapper.count(assetInstitutionBatchParameter);
        Page<AssetInstitutionBatchParameter> page = PagingQuery.getPage(count, assetInstitutionBatchParameter);
        PageVO<AssetInstitutionBatchParameterResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<AssetInstitutionBatchParameter> assetInstitutionBatchParameters = assetInstitutionBatchParameterMapper.queryPage(page);
            List<AssetInstitutionBatchParameterResponse> assetInstitutionBatchParameterResponses = AssetInstitutionBatchParameterConverter.INSTANCE.dosToRes(assetInstitutionBatchParameters);
            pageVO.setResult(assetInstitutionBatchParameterResponses);
        }
        return Result.ok(pageVO);

    }
}
